<?php

namespace App\Http\Requests\Admin\Service;

use App\Exceptions\GeneralException;
use App\Models\Service;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class ServiceDestroy extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     * @throws
     */
    public function authorize()
    {
        $admin = Auth::guard('admin')->user();
        if(!$admin){
            throw new GeneralException('请登录！', 401);
        }
        $service_id = (int)$admin->service_id;
        if ($service_id > 0) {
            $service = Service::query()->where('id', '=', $service_id)->first();
            if(!$service){
                throw new GeneralException('找不到资源', 404);
            }
            if ($service->id == $this->station->id) {
                throw new GeneralException('自己不能删除自己', 403);
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    /**
     * 定义表单规则
     *
     * Date: 11/04/2018
     * @author Tenny
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}
